//
//  TopViewModel.m
//  LittleFairyPoet
//
//  Created by rimi on 15/9/15.
//  Copyright (c) 2015年 周林. All rights reserved.
//

#import "TopViewModel.h"

@implementation TopViewModel
+ (void)addLocalNotificationWithPlanTime:(NSString *)planTime andWithTopTime:(NSString *)topTime {
    NSString * timeStr = [NSString stringWithFormat:@"%@%@", planTime, topTime];
    NSDateFormatter * formattor = [[NSDateFormatter alloc]init];
    formattor.dateFormat = @"yyyy年MM月dd日HH:mm:ss";
    NSDate * topDate = [formattor dateFromString:timeStr];
    UILocalNotification * newNotification = [UILocalNotification new];
        newNotification.timeZone = [NSTimeZone localTimeZone];
        newNotification.fireDate = topDate;
        newNotification.alertBody = @"该学习了！";
        newNotification.alertAction = @"关闭";
        newNotification.repeatInterval = NSCalendarUnitEra;
        newNotification.userInfo = @{@"notificationID" : @"studyTop"};
    newNotification.soundName = UILocalNotificationDefaultSoundName;
    // ios8后，需要添加这个注册，才能得到授权
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        // 通知重复提示的单位，可以是天、周、月
    }
    [[UIApplication sharedApplication] scheduleLocalNotification:newNotification];
}
+ (void)cancelNotification {
    NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
    for (UILocalNotification *notification in localNotifications) {
        NSDictionary *userInfo = notification.userInfo;
        if (userInfo) {
            // 根据设置通知参数时指定的key来获取通知参数
            NSString *info = userInfo[@"studyTop"];
            // 如果找到需要取消的通知，则取消
            if (info != nil) {
                [[UIApplication sharedApplication] cancelLocalNotification:notification];
                break;  
            }  
        }  
    }
}
@end
